﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using Lextm.SharpSnmpLib.Pipeline;
using Lextm.SharpSnmpLib.Objects;
using Lextm.SharpSnmpLib;
using Lextm.SharpSnmpLib.Security;
using System.Reflection;
using System.IO;

namespace SSNMP_Agent
{
    static class Program
    {
        internal static IUnityContainer Container { get; private set; }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Container = new UnityContainer();
            Container.LoadConfiguration("agent");

            // TODO: this is a hack. review it later.
            var store = Container.Resolve<ObjectStore>();
            store.Add(new SysDescr());
            store.Add(new SysObjectId());
            store.Add(new SysUpTime());
            store.Add(new SysContact());
            store.Add(new SysName());
            store.Add(new SysLocation());
            store.Add(new SysServices());
            store.Add(new SysORLastChange());
            store.Add(new SysORTable());
            store.Add(new startStop());

            GetObjectsInAssembly(store);

            var users = Container.Resolve<UserRegistry>();
            users.Add(new OctetString("neither"), DefaultPrivacyProvider.DefaultPair);
            users.Add(new OctetString("authen"), new DefaultPrivacyProvider(new MD5AuthenticationProvider(new OctetString("authentication"))));
            users.Add(new OctetString("privacy"), new DESPrivacyProvider(new OctetString("privacyphrase"),
                                                                         new MD5AuthenticationProvider(new OctetString("authentication"))));

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

        private static void GetObjectsInAssembly(ObjectStore store)
        {
            foreach (string dll in Directory.EnumerateFiles("loadable","*.dll"))
            {
                Assembly assembly = Assembly.LoadFrom(dll);
                foreach (Type item in assembly.GetTypes())
                {
                    if (item.GetInterface(typeof(ISnmpObject).FullName) != null)
                    {
                        store.Add((ISnmpObject)Activator.CreateInstance(item));
                    }
                }
            }
        }
    }
}
